//
//  EmailViewController.m
//  NewMusic
//
//  Created by dllo on 15/11/21.
//  Copyright © 2015年 Going. All rights reserved.
//

#import "EmailViewController.h"
#import "SKPSMTPMessage.h"
#import "NSData+Base64Additions.h"
#import "CustomerNetworking.h"
#import "Reachability.h"
@interface EmailViewController () <SKPSMTPMessageDelegate>
@property (nonatomic, retain) UITextView *textView;
@property (nonatomic, retain) UIImageView *imageView;
@end

@implementation EmailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 创建nav
    [self creatNav];
    // 创建控件
    [self creatSubView];
    if (self.nice)
    {
        self.navigationItem.title = @"感谢对我们的支持";
        self.imageView.image = [UIImage imageNamed:[[NSBundle mainBundle] pathForResource:@"emailNice" ofType:@"jpg"]];
    } else {
        self.navigationItem.title = @"我们会继续努力的";
        self.imageView.image = [UIImage imageNamed:[[NSBundle mainBundle] pathForResource:@"emailPoor" ofType:@"png"]];
    }
    
}
- (void)creatSubView
{
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 150)];
    [self.view addSubview:self.imageView];
    [_imageView release];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(30, 155, 150, 50)];
    label.text = @"输入您的宝贵意见 :";
    label.textColor = [UIColor colorWithRed:220 / 255.0 green:220 / 255.0 blue:220 / 255.0 alpha:1.0];
    label.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:label];
    [label release];
    self.textView = [[UITextView alloc] initWithFrame:CGRectMake(25, 210, [UIScreen mainScreen].bounds.size.width - 50, 150)];
    self.textView.font = [UIFont systemFontOfSize:15];
    self.textView.textColor = [UIColor colorWithRed:80 / 255.0 green:80 / 255.0 blue:80 / 255.0 alpha:1.0];
    self.textView.layer.cornerRadius = 6;
    [self.view addSubview:self.textView];
    [_textView release];
}
- (void)creatNav
{
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_fanhui"] style:UIBarButtonItemStylePlain target:self action:@selector(barLeft)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor colorWithRed:210 / 255.0 green:210 / 255.0 blue:210 / 255.0 alpha:1.0];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(barRight)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor colorWithRed:210 / 255.0 green:210 / 255.0 blue:210 / 255.0 alpha:1.0];
}
- (void)barRight
{
    // 判断当前wangluo
    NetworkStatus currentState = [CustomerNetworking getNetWorkStatus];
    if (currentState == NotReachable)
    {
        [self alertController:nil andOK:@"取消" andtitle:@"提示" andDesc:@"当前没有网络"];
        return;
    }
    if (self.textView.text.length != 0)
    {
        SKPSMTPMessage *message = [SKPSMTPMessage new];
        message.delegate = self;
        [message setSubject:@"反馈信息"];
        [message setToEmail:@"q1653904195@163.com"];
        [message setFromEmail:@"q614651486@163.com"];
        [message setRelayHost:@"smtp.163.com"];
        [message setRequiresAuth:YES];
        [message setLogin:@"q614651486@163.com"];
        [message setPass:@"qiao19931025"];
        [message setWantsSecure:YES];
        NSDictionary * plainPart=[NSDictionary dictionaryWithObjectsAndKeys:@"text/plain",kSKPSMTPPartContentTypeKey, self.textView.text,kSKPSMTPPartMessageKey,@"8bit",kSKPSMTPPartContentTransferEncodingKey, nil];
        message.parts = [NSArray arrayWithObjects:plainPart, nil];
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            [message send];
        });
    } else {
        [self alertController:@"取消" andOK:nil andtitle:@"提示" andDesc:@"请不要发送空信息"];
    }
}
- (void)messageSent:(SKPSMTPMessage *)message
{
    [self alertController:@"取消" andOK:@"好的" andtitle:@"提示" andDesc:@"您的反馈我们已经收到,我们会尽快改善"];
}
- (void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error
{
    [self alertController:@"取消" andOK:nil andtitle:@"提示" andDesc:@"发送失败,请稍后再试"];
}
- (void)alertController:(NSString *)cancel
                  andOK:(NSString *)ok
               andtitle:(NSString *)title
                andDesc:(NSString *)desc
{
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:title message:desc preferredStyle:UIAlertControllerStyleAlert];
    if (cancel.length != 0)
    {
        UIAlertAction *action = [UIAlertAction actionWithTitle:cancel style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alertC addAction:action];
    }
    if (ok.length != 0)
    {
         UIAlertAction *actionO = [UIAlertAction actionWithTitle:ok style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
             dispatch_async(dispatch_get_main_queue(), ^{
                 [self barLeft];
             });
         }];
        [alertC addAction:actionO];
    }
   [self presentViewController:alertC animated:YES completion:^{
       
   }];
}
- (void)barLeft
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
